package com.k8sops.backend.controller;

import com.k8sops.backend.common.DevOpsConstant;
import com.k8sops.backend.common.Result;
import com.k8sops.backend.entity.Menu;
import com.k8sops.backend.service.MenuService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单管理控制器
 */
@RestController
@RequestMapping("/api/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 创建菜单
     * @param menu 菜单信息
     * @return 创建结果
     */
    @PostMapping("/create")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR}, logical = Logical.OR)
    public Result<Menu> createMenu(@RequestBody Menu menu) {
        Menu createdMenu = menuService.createMenu(menu);
        return Result.success(createdMenu);
    }

    /**
     * 更新菜单
     * @param menu 菜单信息
     * @return 更新结果
     */
    @PutMapping("/update")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR}, logical = Logical.OR)
    public Result<Menu> updateMenu(@RequestBody Menu menu) {
        Menu updatedMenu = menuService.updateMenu(menu);
        return Result.success(updatedMenu);
    }

    /**
     * 删除菜单
     * @param menuId 菜单ID
     * @return 删除结果
     */
    @DeleteMapping("/delete/{menuId}")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR}, logical = Logical.OR)
    public Result<Boolean> deleteMenu(@PathVariable Long menuId) {
        boolean deleted = menuService.deleteMenu(menuId);
        return Result.success(deleted);
    }

    /**
     * 获取菜单详情
     * @param menuId 菜单ID
     * @return 菜单详情
     */
    @GetMapping("/detail/{menuId}")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR}, logical = Logical.OR)
    public Result<Menu> getMenuDetail(@PathVariable Long menuId) {
        Menu menu = menuService.getMenuById(menuId);
        return Result.success(menu);
    }

    /**
     * 获取所有菜单
     * @return 菜单列表
     */
    @GetMapping("/list")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR}, logical = Logical.OR)
    public Result<List<Menu>> getAllMenus() {
        List<Menu> menus = menuService.getAllMenus();
        return Result.success(menus);
    }

    /**
     * 获取菜单树结构
     * @return 菜单树结构
     */
    @GetMapping("/tree")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR}, logical = Logical.OR)
    public Result<List<Menu>> getMenuTree() {
        List<Menu> menuTree = menuService.getMenuTree();
        return Result.success(menuTree);
    }

    /**
     * 获取角色的菜单列表
     * @param roleId 角色ID
     * @return 菜单列表
     */
    @GetMapping("/role/{roleId}")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR}, logical = Logical.OR)
    public Result<List<Menu>> getMenusByRoleId(@PathVariable Long roleId) {
        List<Menu> menus = menuService.getMenusByRoleId(roleId);
        return Result.success(menus);
    }

    /**
     * 获取角色的菜单树结构
     * @param roleId 角色ID
     * @return 菜单树结构
     */
    @GetMapping("/tree/role/{roleId}")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR}, logical = Logical.OR)
    public Result<List<Menu>> getMenuTreeByRoleId(@PathVariable Long roleId) {
        List<Menu> menuTree = menuService.getMenuTreeByRoleId(roleId);
        return Result.success(menuTree);
    }
}
